package blogwork;

import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class CardServlet
 */
@WebServlet("/blog_register")
public class blog_register extends HttpServlet {
	private static final long serialVersionUID = 1L;

	private static final String JDBC_URL = "jdbc:mysql://localhost:3306/blog?useUnicode=true&characterEncoding=utf8&useSSL=true";
	private static final String DB_USER = "root";
	private static final String DB_PWD = "shihuang";

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public blog_register() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String name = request.getParameter("name");
		String avt = "/static/img/avt/avtnull.jpg";

		// 1.加载注册驱动
		try {
			Class.forName("com.mysql.jdbc.Driver").newInstance();
		} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
			e.printStackTrace();
		}

		// 2.建立数据连接
		Connection conn;
		try {
			conn = DriverManager.getConnection(JDBC_URL, DB_USER, DB_PWD);

			// 3.执行SQL语句
			// 建立Statement对象
			Statement stmt = conn.createStatement();

			// 做好准备工作之后就可以执行sql语句了
			String sqla = "select * from register where account = '" + account + "'";

			// 执行sql语句
			ResultSet rs = stmt.executeQuery(sqla);

			// 4.处理数据
			boolean exists = false;
			while (rs.next()) {
				exists = true;
				System.out.println("已存在：" + rs.getString("account"));
			}

			response.setCharacterEncoding("utf-8");
			if (!exists) {
				Calendar jcal = new GregorianCalendar();
				jcal.add(Calendar.MONTH, 0);
				jcal.add(Calendar.DAY_OF_MONTH, 0);
				jcal.add(Calendar.HOUR, 0);
				SimpleDateFormat jsdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
				Timestamp register_time=Timestamp.valueOf(jsdf.format(jcal.getTime()));
				String insertSQL = "insert into register(account,password,name,avt,register_time) values(?,?,?,?,?)";
				PreparedStatement pstmt = conn.prepareStatement(insertSQL);
				pstmt.setString(1, account);
				pstmt.setString(2, password);
				pstmt.setString(3, name);
				pstmt.setString(4, avt);
				pstmt.setTimestamp(5, register_time);
				int insertResult = pstmt.executeUpdate();
				
				if (insertResult > 0) {
					response.getWriter().write("注册成功！");
				} else {
					response.getWriter().write("注册失败！");
				}
				
				pstmt.close();
			}else{
				response.getWriter().write("注册失败，已存在同名的！");
			}

			// 5.关闭连接
			rs.close();
			stmt.close();
			conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
